//
//  RooterViewController.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/22.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import HMRefresh

class RooterViewController: UIViewController {
    
    //定义变量判断是否登录
    
    
    var visitorView:VisitorView?
    
    lazy var tableView = UITableView(frame: CGRectZero, style: .Plain)
    //下拉刷新的控件
    lazy var refreshControl = HMRefreshControl()
    
    
    override func loadView() {
        
        
        UserAccount.shardUserAccount.isLogin ? setUpUIView() : setUpVisitorView()
       
       
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        //判断是否登录成功 成功才刷新数据
        
        if UserAccount.shardUserAccount.isLogin{
           loadData()
        }else{
           NSNotificationCenter.defaultCenter().addObserver(self, selector: "UserAccountWillLogin", name: CZWeiboNotification, object: nil)
        }
    }
    
    @objc func UserAccountWillLogin(){
    
       view = nil
        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: CZWeiboNotification, object: nil)
    }

    func loadData(){
 
    }

}

extension RooterViewController: visitorViewDelegate {

    
    //添加监听方法
    func visitorViewDidRegister() {
        print("注册")
        
    }
    func visitorViewDidLogin() {
        print("登录")
        
        let OAuth = OAuthViewController()
        
        let nav = UINavigationController(rootViewController: OAuth)
        
        presentViewController(nav, animated: true, completion: nil)
    }

}

extension RooterViewController{
    
    func setUpVisitorView(){
    
        visitorView = VisitorView()

        view = visitorView
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action:"visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style:.Plain, target: self, action:"visitorViewDidLogin")
    }

    func setUpUIView() {
        
        view = tableView
        //设置数据源
        tableView.dataSource = self
        //设置代理
        tableView.delegate = self
        
        setUpTableView()
        
    }
    ///设置上下拉刷新
    private func setUpTableView(){
        
        //添加下拉刷新
        tableView.addSubview(refreshControl)
        //设置下拉刷新图
        refreshControl.pulldownView = CZRefreshView.refreshView()
        refreshControl.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
    }


}

// MARK -----数据源协议
extension RooterViewController:UITableViewDataSource,UITableViewDelegate {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
    
    
}